//
//  MyCollectionController.m
//  吃货·上海
//
//  Created by lanou on 15/11/30.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "MyCollectionController.h"
#import "ShareCoreData.h"
#import "SHMenuCell.h"
#import "DetailController.h"

@interface MyCollectionController () <UITableViewDataSource , UITableViewDelegate>

@property(nonatomic,strong)UITableView *myTabelView;

@property(nonatomic,strong)NSMutableArray *modelArray;

@property(nonatomic,strong)UIButton *btn;

@end

static NSString *ID = @"cell";
@implementation MyCollectionController
- (NSMutableArray *)modelArray
{
    if (_modelArray == nil) {
        self.modelArray = [NSMutableArray array];
    }
    return _modelArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [self getModelarrayFromDB];
    
    self.myTabelView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.myTabelView.dataSource = self;
    self.myTabelView.delegate = self;

    self.myTabelView.separatorStyle =NO;
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight / 4)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"end";
    [self.myTabelView setTableFooterView:label];
//

    self.myTabelView.rowHeight = KScreenHeight / 3;
    
    [self.myTabelView registerClass:[SHMenuCell class] forCellReuseIdentifier:ID];
    
    [self.view addSubview:_myTabelView];
    
    self.btn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.btn.frame = CGRectMake(0, 0, 30, 20);
    self.btn.selected = NO;
    [_btn setTitle:@"编辑" forState:UIControlStateNormal];
    [_btn setTitle:@"取消" forState:UIControlStateSelected];
    [_btn addTarget:self action:@selector(editBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btn1 = [[UIBarButtonItem alloc]initWithCustomView:_btn];
    self.navigationItem.rightBarButtonItem = btn1;
    
}

- (void)editBtn:(UIButton *)btn
{
    btn.selected = !btn.selected;
    
    for (SHMenuCell *cell in [self.myTabelView visibleCells]) {
        if (btn.selected == YES) {
            cell.deleteBtn.hidden = NO;
            [cell.deleteBtn addTarget:self action:@selector(deleteBtn:) forControlEvents:UIControlEventTouchUpInside];
        }
        else
        {
            cell.deleteBtn.hidden = YES;
        }
    }
    
    [self.myTabelView reloadData];
}

- (void)getModelarrayFromDB
{
  [self.modelArray addObjectsFromArray:[[ShareCoreData shareInstance]selectAllDataWithSortString:nil]];
    
    NSLog(@"%@",_modelArray);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SHMenuCell *cell  =[tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    SHMenuModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    
    if (self.btn.selected == YES ) {
            cell.deleteBtn.hidden = NO;
    }else
    {
        cell.deleteBtn.hidden = YES;
    }
    
    return cell;
}

- (void)deleteBtn:(UIButton *)btn
{
    SHMenuCell *cell = (SHMenuCell *)btn.superview.superview;
    NSIndexPath *indexPath = [_myTabelView indexPathForCell:cell];

    if ([[self.myTabelView indexPathsForVisibleRows] containsObject:indexPath]) {
        SHMenuModel *model = self.modelArray[indexPath.row];
                
        [self.modelArray removeObject:model];
        if ([[ShareCoreData shareInstance]selectOneDataWithTitle:model.title]) {
            [[ShareCoreData shareInstance]deleteDataWithTitle:model.title];
            [self.myTabelView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }
        else
        {
            return;
        }
    }
    [self.myTabelView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SHMenuModel *model = self.modelArray[indexPath.row];
    DetailController *detailVc =[[DetailController alloc]init];
    detailVc.model = model;
    [self.navigationController pushViewController:detailVc animated:NO];
}



@end
